package xyz.mumuwei.youthcourseserver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.mumuwei.youthcourseserver.common.dto.PageDto;
import xyz.mumuwei.youthcourseserver.common.lang.Const;
import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.pojo.UserInfo;
import xyz.mumuwei.youthcourseserver.service.UserInfoService;
import xyz.mumuwei.youthcourseserver.service.UserService;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author mumuwei
 * @date 2022/2/25
 */
@RequestMapping("/student")
@RestController
public class StudentController {

    private UserService userService;
    private UserInfoService userInfoService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setUserInfoService(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    /**
     * 获取教师列表-分页
     */
    @GetMapping("/page")
    public Result page(PageDto pageDto){
        IPage<UserInfo> page = userService.getUserInfoPage(pageDto, Const.STUDENT_ROLE_CODE);
        if(page == null){
            return Result.fail("查询失败!");
        }
        return Result.success(page);
    }

    /**
     * 获取教师列表-不分页
     */
    @GetMapping("/list")
    public Result list(){
        List<UserInfo> list = userService.getUserInfoList(Const.STUDENT_ROLE_CODE);
        if(list == null){
            return Result.fail("查询失败！");
        }
        return Result.success(list);
    }

    /**
     * 创建学生
     * @param userInfo 教师信息
     */
    @PostMapping("")
    public Result create(@Validated UserInfo userInfo){
        // 设置用户角色为学生
        userInfo.setRoleId(Const.STUDENT_ROLE_CODE);
        Integer info = userInfoService.createInfo(userInfo);
        if(info == -1){
            return Result.fail("手机号已被绑定！");
        }
        if(info > 0){
            return Result.success("创建成功！");
        }
        return Result.fail("创建失败！");
    }

    /**
     * 更新教师信息
     */
    @PostMapping("/update")
    public Result update(@Validated UserInfo userInfo){
        Integer info = userInfoService.updateInfo(userInfo);
        if(info == -1){
            return Result.fail("用户id或角色id有误！");
        }
        if(info > 0){
            return Result.success("更新成功");
        }
        return Result.fail("更新失败！");
    }

    @GetMapping("/del_{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空") Integer id){
        if (!userInfoService.getById(id).getRoleId().equals(Const.STUDENT_ROLE_CODE)) {
            return Result.fail("身份不匹配！");
        }
        boolean b = userInfoService.removeById(id);
        if(b){
            return Result.success("删除成功！");
        }
        return Result.fail("删除失败！");
    }
}
